#!/usr/bin/env bash
set -e
set -u
set -o pipefail


get_directory_depth_offset() {
    local path=${1:?}
    local dirs=${path//[!\/]}
    local depth_array=()
    for (( i=1; i<${#dirs}; ++i )); do
        depth_array+=("../")
    done
    echo ./${depth_array[*]// /}
}


get_file_ext() {
    local path=${1:?}
    local filename=$(basename -- "$path")
    echo "${filename##*.}"
}


get_file_type_ext() {
    local file_type=${1:?}
    if [[ "markdown" == "$file_type" ]]; then
        echo "md"
    elif [[ "org" == "$file_type" ]]; then
        echo "org"
    else
        echo "Unsupported file_type $file_type" > /dev/stderr
    fi
}


get_working_ext() {
    local file_type=${1:?}
    local path=${2:?}
    local file_type_ext=$(get_file_type_ext $file_type)
    local extension=$(get_file_ext $path)
    if [[ "$extension" == "$file_type_ext" ]]; then
        echo "html"
    else
        echo ${extension}
    fi
}


get_output_file() {
    local file_type=${1:?}
    local path=${2:?}
    local output_dir=${3:-?}
    local path_sans_ext="${path%.*}"


    local output_file=$output_dir/"${path_sans_ext#*\/}"
    if [[ "" == "$path_sans_ext" ]]; then
        echo ${output_file}$(basename $path)
    elif [[ "$path" != "$path_sans_ext" ]]; then
        echo ${output_file}.$(get_working_ext $file_type $path)
    else
        echo ${output_file}
    fi
}


get_absolute_path() {
    local current_dir=${1:?}
    local relative_dir=${2:?}
    if [[ "/" == "$current_dir" ]]; then
        echo ${current_dir}$relative_dir
    else
        echo ${current_dir}/$relative_dir
    fi
}


get_git_branch() {
    echo `git rev-parse --abbrev-ref HEAD`
}


get_git_commit_id() {
    echo `git rev-parse HEAD`
}


get_git_commit_id_short() {
    echo `git rev-parse --short HEAD`
}


get_date_stamp() {
    echo `date --utc +%Y-%m-%d`
}


process_file() {
    local file_type=${1:?}
    local path=${2:?}
    local template=${3:?}
    local filter=${4:?}
    local theme=${5:?}
    local alt_theme_path=${6:?}
    local output_dir=${7:?}
    local git_branch=${8:?}
    local git_commit_id=${9:?}
    local git_commit_id_short=${10:?}
    local date_stamp=${11:?}


    local output_file=$(get_output_file $file_type $path $output_dir)
    local file_type_ext=$(get_file_type_ext $file_type)
    local extension=$(get_file_ext $path)
    local dir_offset=$(get_directory_depth_offset $path)


    mkdir -p $(dirname ${output_file})
    if [[ "$extension" == "$file_type_ext" ]]; then
        pandoc \
            -f ${file_type} \
            -t html \
            ${path} \
            --template ${template} \
            --lua-filter=${filter} \
            --table-of-contents \
            --number-sections \
            --variable "offset:$dir_offset" \
            --variable "gitbranch:$git_branch" \
            --variable "gitcommitid:$git_commit_id" \
            --variable "gitcommitidshort:$git_commit_id_short" \
            --variable "date:$date_stamp" \
            --variable "theme:$theme" \
            --variable "other-theme:$alt_theme_path" \
            --output ${output_file}
    else
        cp ${path} ${output_file}
    fi
}


build_main() {
    local file_type=${1:?}
    local website_root_dir=${2:?}
    local relative_path_to_template=${3:?}
    local relative_path_to_filter=${4:?}
    local theme=${5:-}
    local alt_theme_path=${6:-}
    local relative_output_dir=${7:-"output"}


    local working_dir=$(pwd)
    local website_template=$(get_absolute_path $working_dir $relative_path_to_template)
    local filter=$(get_absolute_path $working_dir $relative_path_to_filter)
    local output_dir=$(get_absolute_path $working_dir $relative_output_dir)
    local git_branch=$(get_git_branch)
    local git_commit_id=$(get_git_commit_id)
    local git_commit_id_short=$(get_git_commit_id_short)
    local date_stamp=$(get_date_stamp)


    cd ${website_root_dir}
    local array=()
    while IFS=  read -r -d $'\0'; do
        array+=("$REPLY")
    done < <(find . -type f -print0)
    for current_file in "${array[@]}"
    do
        process_file \
            $file_type \
            $current_file \
            $website_template \
            $filter \
            $theme \
            $alt_theme_path \
            $output_dir \
            $git_branch \
            $git_commit_id \
            $git_commit_id_short \
            $date_stamp
    done
}


build_main $@
